package cpa.prowon.gui.actions;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;

import pho.prowon.category.VisibleIndexCategory;
import cpa.prowon.gui.MainWindow;
import cpa.prowon.gui.RenameCategoryWindow;

/**
 * The rename category action is used to rename an index category
 * in the index. The rename will rename the appropriate information in the
 * images which are held by this category
 * @author Chris
 */
public class RenameCategoryAction
	extends PatchedAction
	implements ISelectionChangedListener {

	/**
	 * Constructor
	 * @param MainWindow The window where the action is used
	 * @author
	 */
	public RenameCategoryAction(MainWindow w) {
		super(w);
		setEnabled(false);
	}

	/**
	 * Return the title key
	 * @return The title key
	 * @author Chris
	 */
	protected String getTitleKey() {
		return "menu_item_edit_renamecategory";
	}

	/**
	 * Return the tooltip key
	 * @return The tootip key
	 * @author Chris
	 */
	protected String getTooltipKey() {
		return "menu_item_edit_renamecategory_tooltip";
	}

	/**
	 * Return the image key
	 * @return The image key
	 * @author Chris
	 */
	protected String getImageKey() {
		return "";
	}

	/**
	 * Run the action
	 * @author Chris
	 */
	public void run() {
		
		// Get the selected category
		IStructuredSelection selection = window.getCategorizationViewerSelection();
		
		// Open the rename category window
		RenameCategoryWindow rename_window = new RenameCategoryWindow(window, (VisibleIndexCategory) selection.getFirstElement());
		rename_window.open();
		
		// Refresh the category viewer, in case the category is not needed anymore, it will get removed
		window.refreshCategoryPart();
				
	}
	
	/**
	 * Listen to the selection changed event in order to enable or disable the action
	 * @param SelectionChangedEvent The event that occurs when the selection changed
	 * @author Chris
	 * 
	 */
	public void selectionChanged(SelectionChangedEvent event) {

		// Get the selection and set the action enabled or disabled depending on this
		IStructuredSelection selection = window.getCategorizationViewerSelection();
		if (selection.size() != 1) {
			setEnabled(false);
			return;
		}
		VisibleIndexCategory ic = (VisibleIndexCategory) selection.getFirstElement();
		if (ic.canBeRenamed()) {
			setEnabled(true);
		} else {
			setEnabled(false);
		}

	}
	
}
